package org.wedding.center.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.RandomUtil;
import org.wedding.center.dto.query.CouponQuery;
import org.wedding.center.entity.CouponInfo;
import org.wedding.center.service.CouponInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.wedding.utils.CommonResult;
import org.wedding.dto.PageModel;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.wedding.utils.RedisUtil;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 优惠卷信息表(CouponInfo)表控制层
 *
 * @author songkaiyue
 * @since 2025-05-14 16:30:25
 */
@AllArgsConstructor
@RestController
@RequestMapping("/couponInfo")
@Tag(name = "CouponInfo", description = "优惠卷信息表")
public class CouponInfoController{
    private final CouponInfoService couponInfoService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 测试更新优惠卷过期状态
     * @param timeout
     * @param id
     * @return
     */
    @SaIgnore
    @RequestMapping("/testRedis")
    public String hello(Integer timeout,String id) {
        // 设置键值对，并指定过期时间为 10 秒
//        String key = "coupon:" + RandomUtil.randomNumbers(3);
        String key = "coupon:" + id;
        redisUtil.setValueByTime(key,id,timeout, TimeUnit.SECONDS);
        System.out.println("key: " + key);
        return key;
    }
    
    /**
     * 分页查询所有数据
     *
     * @param query 分页对象
     * @return 所有数据
     */
    @Operation(summary = "分页查询所有数据")
    @RequestMapping(value = "/list", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult<PageModel> list(CouponQuery query) {
        return couponInfoService.findAll(query);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "通过主键查询单条数据")
    @RequestMapping(value = "/view", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult view(String id) {
        return couponInfoService.view(id);
    }

    /**
     * 新增数据
     *
     * @param couponInfo 实体对象
     * @return 新增结果
     */
    @Operation(summary = "新增数据")
    @RequestMapping(value = "/add", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult add(@RequestBody CouponInfo couponInfo) {
        return couponInfoService.add(couponInfo);
    }

    /**
     * 更新已过期优惠券
     * @return
     */
    @Operation(summary = "更新已过期优惠券")
    @RequestMapping(value = "/expired", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult expired() {
        return couponInfoService.expired();
    }

    /**
     * 根据id更新已过期优惠券
     * @return
     */
    @Operation(summary = "根据id更新已过期优惠券")
    @RequestMapping(value = "/expiredById", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult expiredById(String id) {
        return couponInfoService.expiredById(id);
    }

    /**
     * 修改数据
     *
     * @param couponInfo 实体对象
     * @return 修改结果
     */
    @Operation(summary = "修改数据")
    @RequestMapping(value = "/update", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult update(@RequestBody CouponInfo couponInfo) {
        return couponInfoService.updateEntity(couponInfo);
    }

   /**
     * 删除数据
     *
     * @param ids 主键
     * @return 删除结果
     */
    @Operation(summary = "删除数据")
    @RequestMapping(value = "/delete", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult delete(@RequestBody List<String> ids) {
        return couponInfoService.delete(ids);
    }
    
}

